cat (Unix)

The cat command is a standard Unix program used to concatenate and display files. The name is from catenate, a synonym of concatenate.

Contents

Specification

The Single Unix Specification specifies the behavior that the contents of each of the files given in sequence as arguments will be written to the standard output in the same sequence, and mandates one option, -u, where each byte is printed as it is read.

If the filename is specified as -, then cat will read from standard input at that point in the sequence. If no files are specified, cat will read from standard input entered.

Extensions

Both the BSD versions of cat (as per the OpenBSD manpage) and the GNU coreutils version of cat specify the following options:

Unix culture

Jargon File definition

The Jargon File version 4.4.7 lists this as the definition of cat:

  1. To spew an entire file to the screen or some other output sink without pause (syn. blast).
  2. By extension, to dump large amounts of data at an unprepared target or with no intention of browsing it carefully. Usage: considered silly. Rare outside Unix sites. See also dd, BLT.

Among Unix fans, cat(1) is considered an excellent example of user-interface design, because it delivers the file contents without such verbosity as spacing or headers between the files, and because it does not require the files to consist of lines of text, but works with any sort of data.

Among Unix critics, cat(1) is considered the canonical example of bad user-interface design, because of its woefully unobvious name. It is far more often used to blast a single file to standard output than to concatenate two or more files. The name cat for the former operation is just as unintuitive as, say, LISP's cdr.

Useless use of cat

UUOC (from comp.unix.shell on Usenet) stands for "Useless Use of cat". As received wisdom on comp.unix.shell observes "The purpose of cat is to concatenate (or 'catenate') files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process." This is also referred to as "Cat abuse". Nevertheless one sees people doing

 cat filename | cmd arg1 arg2 argn

instead of the (not quite) equivalent and cheaper

 <filename cmd arg1 arg2 argn

or the classical (not quite) equivalent

 cmd arg1 arg2 argn < filename

It is not quite equivalent as the last two examples allows cmd to seek in the file, whereas the first does not.

Without two named files cat is a useless use of cat, with the exception of viewing the contents of a single file on the command line. A UUOC campaign will eliminate the inefficiency from shell scripts by using redirection instead. However, cat with one named file is safer where human error is a concern—one wrong use of the default[1] redirection symbol ">" instead of "<" (often adjacent on keyboards) may permanently delete[2] the file you were just needing to read. Also cat will prevent the command from seeking in the file.

Since 1995, occasional awards for UUOC have been given out, usually by Perl programmer Randal L. Schwartz. There is a web page devoted to this and other similar awards.[3] In British hackerdom the activity of fixing instances of UUOC is sometimes called demoggification.[4]

Binary use

Because cat simply joins files (without adding anything in between), it can be used equally well on binary files as on text files. For many binary formats, the result cannot be parsed (for example, if a file has a unique header or footer) and this use of cat is not especially useful, but for some multimedia container formats the resulting file is valid, and this provides a very simple way of concatenating files, particularly video streams. Significantly, the MPEG program stream (MPEG-1 and MPEG-2) and DV (Digital Video) formats can be so concatenated – such a stream is fundamentally a stream of packets, which can thus be joined.

Further, any other video format can be concatenated by transcoding to one of these privileged formats, concatenating via cat, and then transcoding back. This procedure is in fact endorsed by FFmpeg.[5]

Other operating systems

The equivalent command in the VMS, CP/M, DOS, OS/2, and Microsoft Windows operating system command shells is type.

Note: To combine multiple files together in DOS/Windows you can use the "copy /b" command:

Example:

 copy /b file1.txt + file2.txt file3.txt

This copies file1.txt and file2.txt in binary mode together in one file, file3.txt.

See also

References

  1. ^ The default behavior for redirection is to clobber the file to its immediate right.
  2. ^ More accurately stated ">" will truncate the file.
  3. ^ http://partmaps.org/era/unix/award.html
  4. ^ moggy is chiefly British word for "(mongrel) cat", hence demoggification literally means "removal of (non-special) cats".
  5. ^ FFmpeg FAQ: 3.15 How can I join video files?

External links

Manual pages

Other